﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STLGROUP.Layers.Base;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;

namespace STLGROUP.Layers.BLL
{
    public partial class DT_HoaDonBLL:BaseBLL
    {
        public string GetMaHoaDon(int idHoaDon)
        {
            return "HD" + idHoaDon.ToString().PadLeft(5, '0');
        }

        public string GetTenKhachHangByIDKhachHang(int idKhachHang)
        {
            var data = from s in Cache.Context.DT_KhachHang
                       where s.IDKhachHang == idKhachHang
                       select s;

            DT_KhachHang kh = data.FirstOrDefault();

            return kh.TenDayDu;
        }

        public int? GetIDKhachHangTheoIDDonDatHang(int idDonDatHang)
        {
            var data = from s in Cache.Context.DT_DonDatHang
                       where s.IDDonDatHang == idDonDatHang
                       select s;

            DT_DonDatHang sp = data.FirstOrDefault();

            return sp.IDKhachHang;
        }

        public DT_SanPham GetSanPham(int IDSanPham)
        {
            var data = from s in Cache.Context.DT_SanPham
                       where s.IDSanPham == IDSanPham
                       select s;

            DT_SanPham sp = data.FirstOrDefault();

            return sp;
        }

        public DT_SanPham GetSanPham(string maSanPham)
        {
            var data = from s in Cache.Context.DT_SanPham
                       where s.MaSanPham == maSanPham
                       select s;

            DT_SanPham sp = data.FirstOrDefault();

            return sp;
        }

        public string GetDonViTinh(int? idDonViTinh)
        {
            var data = from s in Cache.Context.DM_DonViTinh
                       where s.IDDonViTinh == idDonViTinh
                       select s;

            DM_DonViTinh dvt = data.FirstOrDefault();

            if (dvt != null)
            {
                return dvt.TenDonViTinh;
            }

            return Const.DON_VI_TINH;
        }

        public decimal GetGiamGiaSanPham(int idSanPham)
        {
            var data = from s in Cache.Context.KM_GiamGiaSanPham
                       where s.IDSanPham == idSanPham
                       select s;

            KM_GiamGiaSanPham giamGia = data.FirstOrDefault();

            if (giamGia != null)
            {
                decimal? giamGiaSP = (decimal)giamGia.HeSoGiamGiam * GetSanPham(idSanPham).GiaBanSi;
                return giamGiaSP.HasValue ? giamGiaSP.Value : 0;
            }

            return 0;
        }
        
        /// <summary>
        ///  Get Hoa Don Da Su Dung
        /// </summary>
        /// <param name="pTuNgay"></param>
        /// <param name="pDenNgay"></param>
        /// <author>viet sang</author>
        /// <returns></returns>
        public List<View_ThongKeDoanhThu> GetHoaDonDaSuDung(DateTime? pTuNgay, DateTime? pDenNgay)
        {
            List<View_ThongKeDoanhThu> data = this.Context.View_ThongKeDoanhThu.ToList();
            if (pTuNgay.HasValue)
            {
                data = data.Where(s => s.NgayTao >= pTuNgay).ToList();
            }
            if (pDenNgay.HasValue)
            {
                data = data.Where(s => s.NgayTao <= pDenNgay).ToList();
            }

            return data;
        }

        //public KM_GiamGiaHoaDon GetGiamGiaHoaDon(
    }
}
